package com.icexxx.ice_browser;

import java.util.ArrayList;
import java.util.List;

import cn.hutool.core.bean.BeanUtil;

public class DataCenter {
    private static List<Request> list = new ArrayList<>();

    public List<Request> getList() {
        return list;
    }

    public void setList(List<Request> list) {
        DataCenter.list = list;
    }

    public static List<Request> add(Request value) {
        DataCenter.list.add(value);
        return getAll();
    }

    public static List<Request> getAll() {
        return list;
    }

    public static Request findById(String id) {
        for (int i = 0; i < list.size(); i++) {
            Request request = list.get(i);
            if (request != null) {
                String idCur = request.getId();
                if (id.equals(idCur)) {
                    return request;
                }
            }
        }
        return null;
    }

    public static void update(Request request) {
        String id = request.getId();
        for (int i = 0; i < list.size(); i++) {
            Request requestCur = list.get(i);
            if (requestCur != null) {
                String idCur = requestCur.getId();
                if (id.equals(idCur)) {
                    BeanUtil.copyProperties(request, requestCur, "id");
                    long updateTime = System.currentTimeMillis();
                    requestCur.setUpdateTime(updateTime);
                }
            }
        }
    }

    public static void addAll(List<Request> all) {
        list.addAll(all);
    }

    public static Request first() {
        return list.get(list.size() - 1);
    }
}
